<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

class DownloadLog extends Model
{
    //
    protected $table = 'download_log';
    protected $guarded = [];


    protected static function boot()
    {
        parent::boot();

        Relation::morphMap([
            '1' => 'App\Models\Home\Client',
            '2' => 'App\Models\Home\Resume'
        ]);
    }

    public function user(){

        return $this->belongsTo('App\Models\Home\Users','uid','id');

    }

    public function DownloadLog(){

        return $this->morphTo('DownloadLog','type','cid');

    }

    public function getList($skip,$limit,$opt=array()){

        if (!isset($limit)||!isset($skip)){
            return [];
        }
        if (!isset($skip)){
            $skip = 0;
        }

        $query = self::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        $list = $query->skip($skip)->limit($limit)->with('DownloadLog')->get();

        return $list;
    }


    public function getCount($opt=array()){

        $query = self::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        return $query->count();
    }
}
